package com.engine.shunsheng.util;

import weaver.general.BaseBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author chenwnj
 * @date 2023/11/14
 * @description
 **/
public class SsUtil {

    BaseBean basebean = new BaseBean();

    /**
     * 获取当前日期是周几
     * 顺序：{"周日","周一","周二","周三","周四","周五","周六"}
     * @return
     */
    public String getNumberOfWeek(String date){
        SimpleDateFormat SDF_DATE_DAY = new SimpleDateFormat("yyyy'-'MM'-'dd");
        String number = "";
//        final String[] WEEK_STR = {"周日","周一","周二","周三","周四","周五","周六"};
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(SDF_DATE_DAY.parse(date));
            number = "" + (calendar.get(Calendar.DAY_OF_WEEK) - 1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return number;
    }

    /**
     * 计算提前、延后分钟数的时间
     */
    public String getCalTime(String time, Integer duration) {
        String result = "";
        try {

            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            Date date = new SimpleDateFormat("HH:mm").parse(time);
            Calendar now = Calendar.getInstance();
            now.setTime(date);
            now.add(Calendar.MINUTE,duration);
            Date afterFiveMin = now.getTime();
            result = format.format(afterFiveMin);
        }catch ( Exception e ) {
            basebean.writeLog("getCalTime exception: " + e);
        }
        return result;
    }

    /**
     * 获取两个时间相差分钟数
     */
    public long getTime(String oldTime,String newTime) {

        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        long diff = 0;
        try {
            long NTime =df.parse(newTime).getTime();
            //从对象中拿到时间
            long OTime = df.parse(oldTime).getTime();
            diff=(NTime-OTime)/1000/60;
        }catch (Exception e) {
            basebean.writeLog("getTime exception: " + e);
        }

        return diff;
    }

}
